package com.github.niefy.modules.wx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.niefy.common.utils.PageUtils;
import com.github.niefy.common.utils.Query;
import com.github.niefy.modules.wx.dao.AnimalNewsMapper;
import com.github.niefy.modules.wx.entity.AnimalNews;
import com.github.niefy.modules.wx.service.AnimalNewsService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author Nifury
 * @date 2017-10-27
 */
@Service
public class AnimalNewsServiceImpl extends ServiceImpl<AnimalNewsMapper, AnimalNews> implements AnimalNewsService {
    private static final String ID_PLACEHOLDER = "${articleId}";
    @Resource
    AnimalNewsMapper animalNewsMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String title = (String) params.get("title");
        String type = (String) params.get("type");
        IPage<AnimalNews> page = this.page(
            new Query<AnimalNews>().getPage(params),
            new QueryWrapper<AnimalNews>()
                .eq(StringUtils.hasText(type), "article_type", type)
                .like(StringUtils.hasText(title), "title", title)
        );
        return new PageUtils(page);
    }

    @Override
    public boolean save(AnimalNews animalNews) {
        if(org.apache.commons.lang3.StringUtils.isNotEmpty(animalNews.getId())){
            animalNewsMapper.updateById(animalNews);
        }else{
            animalNewsMapper.insert(animalNews);
        }
        return true;
    }
}
